//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "BasketPayoff.h"
using namespace Cephei::QL::Instruments;
#include <gen/QL/Math/Array.h>
#include <gen/QL/Payoff.h>
using namespace Cephei::QL::Math;
using namespace Cephei::QL;
#undef HANDLE
#define ABSTRACT
#undef STRUCT
Cephei::QL::Instruments::CBasketPayoff::CBasketPayoff (boost::shared_ptr<QuantLib::BasketPayoff>& childNative, Object^ owner) : CPayoff(CBasketPayoff::typeid)
{
#ifdef HANDLE
	_phBasketPayoff = NULL;
#endif
	_ppBasketPayoff = &childNative;
    _ppPayoff = new boost::shared_ptr<QuantLib::Payoff> (boost::dynamic_pointer_cast<QuantLib::Payoff> (*_ppBasketPayoff));
}
Cephei::QL::Instruments::CBasketPayoff::CBasketPayoff (QuantLib::BasketPayoff& childNative, Object^ owner) : CPayoff(CBasketPayoff::typeid)
{
#ifdef HANDLE
	_phBasketPayoff = NULL;
#endif
	_ppBasketPayoff = new boost::shared_ptr<QuantLib::BasketPayoff> (&childNative);
    _ppPayoff = new boost::shared_ptr<QuantLib::Payoff> (boost::dynamic_pointer_cast<QuantLib::Payoff> (*_ppBasketPayoff));
    _BasketPayoffOwner = owner;
    _PayoffOwner = owner;
}

Cephei::QL::Instruments::CBasketPayoff::CBasketPayoff (CBasketPayoff^ copy) : CPayoff(CBasketPayoff::typeid)
{
#ifdef HANDLE
	_phBasketPayoff = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppBasketPayoff = new boost::shared_ptr<QuantLib::BasketPayoff> (copy->GetShared());
        _ppPayoff = new boost::shared_ptr<QuantLib::Payoff> (boost::dynamic_pointer_cast<QuantLib::Payoff> (*_ppBasketPayoff));
    }
}
Cephei::QL::Instruments::CBasketPayoff::CBasketPayoff (PLATFORM::Type^ t) : CPayoff(CBasketPayoff::typeid)
{
#ifdef HANDLE
	_phBasketPayoff = NULL;
#endif
	if (!t->IsSubclassOf(CBasketPayoff::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Instruments::CBasketPayoff::CBasketPayoff (QuantLib::Handle<QuantLib::BasketPayoff>& childNative, Object^ owner)  : CPayoff(CBasketPayoff::typeid)
{
	_phBasketPayoff = &childNative;
	_ppBasketPayoff = &static_cast<boost::shared_ptr<QuantLib::BasketPayoff>>(childNative.currentLink());
    _ppPayoff = new boost::shared_ptr<QuantLib::Payoff> (boost::dynamic_pointer_cast<QuantLib::Payoff> (*_ppBasketPayoff));
    _BasketPayoffOwner = owner;
}
Cephei::QL::Instruments::CBasketPayoff::CBasketPayoff (QuantLib::Handle<QuantLib::BasketPayoff> childNative)  : CPayoff(CBasketPayoff::typeid)
{
	_phBasketPayoff = &childNative;
	_ppBasketPayoff = &static_cast<boost::shared_ptr<QuantLib::BasketPayoff>>(childNative.currentLink());
    _ppPayoff = new boost::shared_ptr<QuantLib::Payoff> (boost::dynamic_pointer_cast<QuantLib::Payoff> (*_ppBasketPayoff));
}
#endif
#ifdef STRUCT
Cephei::QL::Instruments::CBasketPayoff::CBasketPayoff (QuantLib::BasketPayoff childNative)  : CPayoff(CBasketPayoff::typeid)
{
#ifdef HANDLE
	_phBasketPayoff = NULL;
#endif
	_ppBasketPayoff = new boost::shared_ptr<QuantLib::BasketPayoff> (new QuantLib::BasketPayoff (childNative));
    _ppPayoff = new boost::shared_ptr<QuantLib::Payoff> (boost::dynamic_pointer_cast<QuantLib::Payoff> (*_ppBasketPayoff));
}
#endif

Cephei::QL::Instruments::CBasketPayoff::~CBasketPayoff ()
{
    if (_ppBasketPayoff != NULL)
    {
	    delete _ppBasketPayoff;
        _ppBasketPayoff = NULL;
    }
}
Cephei::QL::Instruments::CBasketPayoff::!CBasketPayoff ()
{
    if (_ppBasketPayoff != NULL)
    {
	    delete _ppBasketPayoff;
    }
}
QuantLib::BasketPayoff& Cephei::QL::Instruments::CBasketPayoff::GetReference ()
{
    if (_ppBasketPayoff == NULL) throw REFNEW NativeNullException ();
	return **_ppBasketPayoff;
}
boost::shared_ptr<QuantLib::BasketPayoff>& Cephei::QL::Instruments::CBasketPayoff::GetShared ()
{
    if (_ppBasketPayoff == NULL) throw REFNEW NativeNullException ();
	return *_ppBasketPayoff;
}
QuantLib::BasketPayoff* Cephei::QL::Instruments::CBasketPayoff::GetPointer ()
{
    if (_ppBasketPayoff == NULL) throw REFNEW NativeNullException ();
	return &**_ppBasketPayoff;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::BasketPayoff>& Cephei::QL::Instruments::CBasketPayoff::GetHandle ()
{
	if (_phBasketPayoff == NULL)
	{
		_phBasketPayoff = new Handle<QuantLib::BasketPayoff> (*_ppBasketPayoff);
	}
	return *_phBasketPayoff;
}
#endif
bool Cephei::QL::Instruments::CBasketPayoff::HasNative () 
{
	return (_ppBasketPayoff != NULL);
}

Double Cephei::QL::Instruments::CBasketPayoff::Accumulate (Cephei::QL::Math::IArray^ a)
{
    CArray^ _Ca;
    try
    {
        _Ca = safe_cast<CArray^> (a);
        _Ca->Lock();
        QuantLib::Array& _a = static_cast<QuantLib::Array&> (_Ca->GetReference ()); 
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppBasketPayoff)->accumulate ( _a );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Ca != nullptr) _Ca->Unlock();
    }
}
Cephei::QL::IPayoff^ Cephei::QL::Instruments::CBasketPayoff::BasePayoff::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	boost::shared_ptr<QuantLib::Payoff> _rv = (boost::shared_ptr<QuantLib::Payoff>)(*_ppBasketPayoff)->basePayoff ( );   
        Cephei::QL::CPayoff^ _nrv = REFNEW Cephei::QL::CPayoff (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
String^ Cephei::QL::Instruments::CBasketPayoff::Description::get ()
{
    try
    {
    	std::string _rv = (std::string)(*_ppBasketPayoff)->description ( );   
        String^ _nrv = (String^)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
String^ Cephei::QL::Instruments::CBasketPayoff::Name::get ()
{
    try
    {
    	std::string _rv = (std::string)(*_ppBasketPayoff)->name ( );   
        String^ _nrv = (String^)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

